<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="标签名" clearable style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="星级">
		    <el-select v-model="dataForm.starClass" placeholder="星级">
		    <el-option label="全部" value="" ></el-option>
		    <el-option label="一星" value="1" ></el-option>
        <el-option label="二星" value="2" ></el-option>
        <el-option label="三星" value="3" ></el-option>
        <el-option label="四星" value="4" ></el-option>
        <el-option label="五星" value="5" ></el-option>
		    </el-select>
	    </el-form-item>
        <el-button v-if="isAuth('good:comment:addEvaluateClass')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!-- <el-button v-if="isAuth('goods:shoppinggoods:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="starClass"
        header-align="center"
        align="center"
        label="标签星级">
        <template slot-scope="scope">
          <el-rate
            v-model="scope.row.starClass"
            :colors="colors">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column
        prop="starContent"
        header-align="center"
        align="center"
        label="评价内客">
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        align="center"
        label="评价标签">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="sort"
        header-align="center"
        align="center"
        label="标签排序">
        <template slot-scope="scope">
          <el-input v-model="scope.row.sort" type="number" @change="updateSort(scope.row.evaluateId, scope.row.sort)"/>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.evaluateId)">查看</el-button>
          <el-button type="text" size="small" @click="deleteComment(scope.row.evaluateId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './evaluate-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          keywords: '',
          starClass: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        colors: ['#99A9BF', '#F7BA2A', '#FF9900']
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(`/good/comment/queryEvaluateClassList`),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'starClass': this.dataForm.starClass
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      deleteComment (commentIds) {
        this.$http({
          url: this.$http.adornUrl(`/good/comment/removeEvaluateClassById`),
          method: 'post',
          data: this.$http.adornData(commentIds, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>
